package com.ruoyi.fly.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.fly.domain.Vehicles;
import com.ruoyi.fly.domain.vo.Examine;
import com.ruoyi.fly.domain.vo.VehiclesUpdateVo;
import com.ruoyi.fly.domain.vo.VehiclesVo;
import com.ruoyi.fly.mapper.VehiclesMapper;
import com.ruoyi.fly.service.VehiclesService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * 车辆信息Service业务层处理
 * @date 2024-04-25
 */
@Service
public class VehiclesServiceImpl implements VehiclesService {
    @Autowired
    private VehiclesMapper vehiclesMapper;

    @Autowired
    private ISysUserService sysUserService;

//    @Autowired
//    private PcVehiclesJoinMapper pcVehiclesJoinMapper;

    /**
     * 查询车辆信息
     *
     * @param id 车辆信息主键
     * @return 车辆信息
     */
    @Override
    public VehiclesVo selectPcVehiclesById(String id) {
        Vehicles vehicles = vehiclesMapper.selectPcVehiclesById(id);
        VehiclesVo vehiclesVo = new VehiclesVo();
        BeanUtils.copyProperties(vehicles,vehiclesVo);
        if (sysUserService.selectUserById(vehicles.getApplyUser()) != null){
            vehiclesVo.setApplyUser(sysUserService.selectUserById(vehicles.getApplyUser()).getNickName());
        }
        return vehiclesVo;
    }

    /**
     * 查询车辆信息列表
     * @return 车辆信息
     */
    @Override
    public List<VehiclesVo> selectPcVehiclesList(Vehicles vehicles) {
        List<Vehicles> pcVehicles1 = vehiclesMapper.selectPcVehiclesList(vehicles);
        List<VehiclesVo> pcVehicles = new ArrayList<>();
        VehiclesVo vehiclesVo = new VehiclesVo();
        pcVehicles1.forEach(vehicles1 -> {
            BeanUtils.copyProperties(vehicles1,vehiclesVo);
            if (sysUserService.selectUserById(vehicles1.getApplyUser()) != null){
                vehiclesVo.setApplyUser(sysUserService.selectUserById(vehicles1.getApplyUser()).getNickName());
            }
            pcVehicles.add(vehiclesVo);
        });
        return pcVehicles;
    }

    /**
     * 新增车辆信息
     * @param vehiclesVo 车辆信息
     * @return
     */
    @Override
    public R insertPcVehicles(VehiclesVo vehiclesVo) {
        vehiclesVo.setCreateTime(DateUtils.getNowDate());
        vehiclesVo.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return vehiclesMapper.insertPcVehicles(vehiclesVo)>0?R.ok("新增成功"):R.fail("操作失败");
    }



    /**
     * 修改车辆信息
     *
     * @param vehiclesUpdateVo 车辆信息
     * @return 结果
     */
    @Override
    public R updatePcVehicles(VehiclesUpdateVo vehiclesUpdateVo) {
        vehiclesUpdateVo.setUpdateBy(SecurityUtils.getNickName());
        vehiclesUpdateVo.setUpdateTime(DateUtils.getNowDate());
        vehiclesUpdateVo.setApplyUser(String.valueOf(SecurityUtils.getUserId()));
        return vehiclesMapper.updatePcVehicles(vehiclesUpdateVo)>0?R.ok("修改成功"):R.fail("操作失败");
    }


    /**
     * 删除车辆信息信息
     *
     * @param id 车辆信息主键
     * @return 结果
     */
    @Override
    public R deletePcVehiclesById(String id) {
        return vehiclesMapper.deletePcVehiclesById(id)>0?R.ok("删除成功"):R.fail("操作失败");
    }

    @Override
    public R deletePcVehiclesByIds(String[] id) {
        return vehiclesMapper.deletePcVehiclesByIds(id)>0?R.ok("删除成功"):R.fail("操作失败");
    }
}
